在 ES6 之後出現了 let 和 const ,用來取代原先使用的 var 來定義變數
var
let
const
function test(){
var a = 'Hi';
}
conosle.log(a) // a is not defined
if(true){
var b = 'Hello'
}
conosle.log(b) //'Hello'
function test(){
var a = 1;
if(true){
var a = 5;
console.log(a);
}
console.log(a);
}
test();
//5
//5
function test(){
let a = 1;
a = 5;
console.log(a);
}
test();
//5
function test(){
const a = 1;
a = 5;
console.log(a);
}
test();
// Uncaught TypeError:Assignment to constant variable.